package com.zhx.ad.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider;
import com.zhx.ad.common.dump.DConstant;
import com.zhx.ad.common.dump.table.*;
import com.zhx.ad.entity.AdCreative;
import com.zhx.ad.entity.AdPlan;
import com.zhx.ad.entity.AdUnit;
import com.zhx.ad.entity.unitcondition.AdUnitIt;
import com.zhx.ad.entity.unitcondition.AdUnitKeyword;
import com.zhx.ad.entity.unitcondition.AdUnitdistrict;
import com.zhx.ad.entity.unitcondition.CreativeUnit;
import com.zhx.ad.enums.CommonStatus;
import com.zhx.ad.mapper.AdCreativcMapper;
import com.zhx.ad.mapper.AdPlanMapper;
import com.zhx.ad.mapper.AdUnitMapper;
import com.zhx.ad.mapper.unitcondition.AdUnitDistrictMapper;
import com.zhx.ad.mapper.unitcondition.AdUnitItMapper;
import com.zhx.ad.mapper.unitcondition.AdUnitKeyworkMapper;
import com.zhx.ad.mapper.unitcondition.CreativeUnitMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.zhx.ad.Application;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-05-11 17:10
 * @description：用途:
 * @modified By：zhanghaixuan
 * @version: 1.0
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class DumpTableService {

    @Autowired
    private AdPlanMapper adPlanMapper;

    @Autowired
    private AdUnitMapper adUnitMapper;

    @Autowired
    private AdCreativcMapper adCreativcMapper;

    @Autowired
    private AdUnitKeyworkMapper adUnitKeyworkMapper;

    @Autowired
    private AdUnitDistrictMapper adUnitDistrictMapper;

    @Autowired
    private AdUnitItMapper adUnitItMapper;

    @Autowired
    private CreativeUnitMapper creativeUnitMapper;


    @Test
    public void dumpTablesData() {
        dumpAdPlanTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_PLAN));
        dumpAdUnitTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_unit));
        dumpAdCreativcTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_CREATIVE));
        dumpAdCreativeUnitTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_CREATIVE_UNIT));
        dumpAdUnitDistrictTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_UNIT_DISTRICT));
        dumpAdUnitItTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_UNIT_IT));
        dumpAdUnitKeyworkTable(String.format("%s%s", DConstant.DATA_ROOT_DIR, DConstant.AD_UNIT_KEYWORD));
    }


    private void dumpAdPlanTable(String fileName) {

        List<AdPlan> allByPlanStatus = adPlanMapper.findAllByPlanStatus(CommonStatus.VALID.getStstus());


        if (CollectionUtils.isEmpty(allByPlanStatus)) {
            return;
        }


        List<AdPlanTable> list = new ArrayList<>();

        allByPlanStatus.forEach(p -> list.add(new AdPlanTable(
                p.getId(),
                p.getUserId(),
                p.getPlanStatus(),
                p.getStartDate(),
                p.getEndDate()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdPlanTable adPlanTable : list) {
                writer.write(JSON.toJSONString(adPlanTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }

    private void dumpAdUnitTable(String fileName) {

        List<AdUnit> allByUnitStatus = adUnitMapper.findAllByUnitStatus(CommonStatus.VALID.getStstus());

        if (CollectionUtils.isEmpty(allByUnitStatus)) {
            return;
        }


        List<AdUnitTable> list = new ArrayList<>();

        allByUnitStatus.forEach(p -> list.add(new AdUnitTable(
                p.getId(),
                p.getPlanId(),
                p.getUnitName(),
                p.getPositionType(),
                p.getUnitStatus()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdUnitTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }

    private void dumpAdCreativcTable(String fileName) {

        List<AdCreative> adCreatives = adCreativcMapper.selectList(new QueryWrapper<>());


        if (CollectionUtils.isEmpty(adCreatives)) {
            return;
        }


        List<AdCreativeTable> list = new ArrayList<>();

        adCreatives.forEach(p -> list.add(new AdCreativeTable(
                p.getId(),
                p.getName(),
                p.getType(),
                p.getMaterialType(),
                p.getHeight(),
                p.getWidth(),
                p.getAuditStatus(),
                p.getUrl()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdCreativeTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }

    private void dumpAdUnitKeyworkTable(String fileName) {

        List<AdUnitKeyword> adUnitKeywords = adUnitKeyworkMapper.selectList(new QueryWrapper<>());

        if (CollectionUtils.isEmpty(adUnitKeywords)) {
            return;
        }

        List<AdUnitKeywordTable> list = new ArrayList<>();

        adUnitKeywords.forEach(p -> list.add(new AdUnitKeywordTable(
                p.getUnitId(),
                p.getKeyword()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdUnitKeywordTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }

    private void dumpAdCreativeUnitTable(String fileName) {

        List<CreativeUnit> creativeUnits = creativeUnitMapper.selectList(new QueryWrapper<>());


        if (CollectionUtils.isEmpty(creativeUnits)) {
            return;
        }


        List<AdCreativeUnitTable> list = new ArrayList<>();

        creativeUnits.forEach(p -> list.add(new AdCreativeUnitTable(
                p.getUnitId(),
                p.getCreativeId()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdCreativeUnitTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }


    private void dumpAdUnitDistrictTable(String fileName) {

        List<AdUnitdistrict> adUnitdistricts = adUnitDistrictMapper.selectList(new QueryWrapper<>());

        if (CollectionUtils.isEmpty(adUnitdistricts)) {
            return;
        }

        List<AdUnitDistrictTable> list = new ArrayList<>();

        adUnitdistricts.forEach(p -> list.add(new AdUnitDistrictTable(
                p.getUnitId(),
                p.getProvince(),
                p.getCity()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdUnitDistrictTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }


    private void dumpAdUnitItTable(String fileName) {

        List<AdUnitIt> adUnitIts = adUnitItMapper.selectList(new QueryWrapper<>());

        if (CollectionUtils.isEmpty(adUnitIts)) {
            return;
        }

        List<AdUnitItTable> list = new ArrayList<>();

        adUnitIts.forEach(p -> list.add(new AdUnitItTable(
                p.getUnitId(),
                p.getItTag()
        )));

        Path path = Paths.get(fileName);

        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (AdUnitItTable adUnitTable : list) {
                writer.write(JSON.toJSONString(adUnitTable));
                writer.newLine();
            }
            writer.close();
        } catch (IOException ex) {
            log.error("dumpAdPlanTable error + fileName");
        }
    }
}
